//
//  HOAreaEntity.m
//  微度报修
//
//  Created by foguo-mac-1 on 6/21/16.
//  Copyright © 2016 WeiDu. All rights reserved.
//

#import "HOAreaEntity.h"
#import "HOConstants.h"
#import "HOStaticDataMgr.h"

@implementation HOAreaEntity

- (instancetype)init
{
	self = [super init];
	if(self)
	{
		self.subAreaList = [[[NSMutableArray alloc] init] autorelease];
	}
	return self;
}

- (void)dealloc
{
	[self.subAreaList removeAllObjects];
	self.subAreaList = nil;
	self.areaName = nil;
	[super dealloc];
}

- (void)parseDic:(NSDictionary *)dic
{
	[self parseDic:dic forSearch:NO];
}

- (void)parseDic:(NSDictionary *)dic forSearch:(BOOL)isForSearch
{
	if(![self isValidatedDic:dic])
		return;
//	[self.subAreaList removeAllObjects];
	self.areaId = [[dic objectForKey:kHO_FIELD_AreaId] intValue];
	self.areaName = [dic objectForKey:kHO_FIELD_AreaName];
	NSArray *subAreaArray = [dic objectForKey:kHO_FIELD_Buildings];
	if(subAreaArray == nil)
		return;
	for(int i = 0; i < [subAreaArray count]; i ++)
	{
		NSDictionary *subDic = [subAreaArray objectAtIndex:i];
		int Id = [[subDic objectForKey:kHO_FIELD_AreaId] intValue];
		HOAreaEntity *entity = nil;
		if(isForSearch)
		{
			entity = [[[HOAreaEntity alloc] init] autorelease];
		}
		else
		{
			entity = [[HOStaticDataMgr sharedInstance] getAreaEntityWithId:Id];
		}
		entity.index = i;
		[entity parseDic:subDic forSearch:isForSearch];
		if(![self.subAreaList containsObject:entity])
			[self.subAreaList addObject:entity];
	}
}

- (void)cleanup
{
	for(int i = 0; i < [self.subAreaList count]; i ++)
	{
		HOAreaEntity *entity = [self.subAreaList objectAtIndex:i];
		[entity cleanup];
	}
	[self.subAreaList removeAllObjects];
}
- (NSString *)description
{
	return [NSString stringWithFormat:@"%p %d %@", [super description], self.areaId, self.areaName];
}
@end
